/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dabarobjects.mobile.ui.actions;

import com.dabarobjects.mobile.bluetooth.PhoneBluetoothConnector;
import com.dabarobjects.mobile.bluetooth.SerialPortAgent;
import com.dabarobjects.ui.surfaces.actions.ExtendedUIAction;
import com.dabarobjects.ui.surfaces.actions.NiceOperationWaitingUI;
import javax.bluetooth.RemoteDevice;

/**
 *
 * @author DabarObjects Solutions (www.dabarobjects.com)
 */
public class CreateServicePortAction extends ExtendedUIAction {

    private RemoteDevice rtDevice;
    private ActionResultListener<String> action;

    public CreateServicePortAction(RemoteDevice dv, ActionResultListener<String> action) {
        super("Open Device Channel For Service");
        this.rtDevice = dv;
        this.action = action;
    }

    @Override
    public void performAction(NiceOperationWaitingUI ops) {
        ops.putMessage("Validating Service Options on Device");
        try {
            PhoneBluetoothConnector cManager = new PhoneBluetoothConnector();
            if (rtDevice != null) {
                SerialPortAgent agent = new SerialPortAgent();
                agent.initialize();
                cManager.searchServices(agent, rtDevice);
                String conURL = cManager.getConnectionURL();
                if(action != null){
                    action.onActionComplete(conURL);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ops.notifyBeforeClosing("Could Not Initialize The Service On This Device Channel");
        } finally {
        }
        ops.complete();
    }
}
